<?php

namespace App\Admin\Controllers;

use App\Models\InvitationCode;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class InvitationCodeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('邀请码');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('邀请码');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('邀请码');
            $content->description('新增');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(InvitationCode::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->model()->where([['is_delete', '=', 0]]);
            $grid->id('ID')->sortable();
            $grid->code('邀请码');
            $grid->desc('描述');
            $grid->expire_time('失效时间')->display(function ($expire_time) {
               return strtotime($expire_time) > time() ? "<span class=\"label label-primary\">" . $expire_time . "</span>" : "<span class=\"label label-danger\">" . $expire_time . "</span>" ;
            });
            $grid->created_at('创建时间');

            $grid->disableExport();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                $filter->like('code', '邀请码');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(InvitationCode::class, function (Form $form) {
            $form->text('code', '邀请码')->rules('required|min:6|max:20', [
                'required' => '邀请码不能为空',
                'min' => '邀请码长度不能小于6位',
                'max' => '邀请码长度不能大于20位'
            ]);
            $form->datetime('expire_time', '失效时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required|date')->default(date("Y-m-d", strtotime("+7 day")));
            $form->text('desc', '描述');
            $form->saving(function (Form $form) {
                $code = $form->code;
                $is_exist = InvitationCode::where(['code' => $code, 'is_delete' => 0])->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '邀请码已存在',
                      ]);

                    return back()->with(compact('error'));
                }
                $form->expire_time = strtotime($form->expire_time);
                $form->desc = $form->desc ? : '';
            });
        });
    }
}
